//
//  HCMainViewController.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCMainViewController.h"
#import <UIKit/UITabBarController.h>

#import "HCCallListViewController.h"
#import "HCCRMViewController.h"
#import "HCKPIViewController.h"
#import "HCCallRecordsViewController.h"
#import "HCMoreViewController.h"
#import "HCLoginPageViewController.h"


//导航title颜色
#define NAVAGATINONBAR_COLOR    [UIColor colorWithRed:0.0/255.0 green:148.0/255.0 blue:163.0/255.0 alpha:1]

HCMainViewController* g_mainViewController;

@interface HCMainViewController ()<UITabBarControllerDelegate>

@property (nonatomic, retain)UITabBarController* tabBarController;
@property (nonatomic, retain)HCCallListViewController* callListViewController;
@property (nonatomic, retain)HCCRMViewController* CRMViewController;
@property (nonatomic, retain)HCKPIViewController* kpiViewController;
@property (nonatomic, retain)HCCallRecordsViewController* callRecordsViewController;
@property (nonatomic, retain)HCMoreViewController* moreViewController;

@property (nonatomic, retain)HCLoginPageViewController* loginViewController;
@property (nonatomic, assign)BOOL isShowMenu;

@end

@implementation HCMainViewController

@synthesize tabBarController = m_tabBarController;
@synthesize callListViewController = m_callListViewController;
@synthesize CRMViewController = m_CRMViewController;
@synthesize kpiViewController = m_kpiViewController;
@synthesize callRecordsViewController = m_callRecordsViewController;
@synthesize moreViewController = m_moreViewController;

@synthesize loginViewController = m_loginViewController;
@synthesize menuView = m_menuView;
@synthesize isShowMenu = m_isShowMenu;

- (id)initWithWindow:(UIWindow*)window
{
    self = [super init];
    if (self != nil)
    {
        window.rootViewController = self;
        [window addSubview:self.view];
        
        [self showLoginView];
        g_mainViewController = self;
//        [self showMainView];
//        [self.view addSubview:self.tabBarController.view];
        m_isShowMenu = NO;
    }
    return self;
}

- (void)dealloc
{
    self.callListViewController = nil;
    self.CRMViewController = nil;
    self.kpiViewController = nil;
    self.callRecordsViewController = nil;
    self.moreViewController = nil;
    
    self.tabBarController = nil;
    
    self.loginViewController = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showLoginView
{
    if (nil == self.loginViewController)
    {
        m_loginViewController = [[HCLoginPageViewController alloc] init];
    }
    
    [self.view addSubview:self.loginViewController.view];
//    [self presentViewController:self.loginViewController animated:YES completion:nil];
}

- (void)showMainView
{
    if (nil == self.tabBarController)
    {
        m_callListViewController = [[HCCallListViewController alloc] init];
        self.callListViewController.title = @"话单";
        UIImage *imgIcon = [UIImage imageNamed:@"nav_bill.png"];
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"话单" image:imgIcon tag:1];
        //        [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
        //           withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
        self.callListViewController.tabBarItem = item;
        [item release];
        
        m_CRMViewController = [[HCCRMViewController alloc] init];
        self.CRMViewController.title = @"CRM";
        imgIcon = [UIImage imageNamed:@"nav_crm.png"];
        item = [[UITabBarItem alloc] initWithTitle:@"CRM" image:imgIcon tag:2];
        //        [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
        //           withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
        self.CRMViewController.tabBarItem = item;
        [item release];
        
        m_kpiViewController = [[HCKPIViewController alloc] init];
        self.kpiViewController.title = @"KPI";
        imgIcon = [UIImage imageNamed:@"nav_schedule.png"];
        item = [[UITabBarItem alloc] initWithTitle:@"微提醒" image:imgIcon tag:3];
        //        [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
        //           withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
        self.kpiViewController.tabBarItem = item;
        [item release];
        
        m_callRecordsViewController = [[HCCallRecordsViewController alloc] init];
        self.callRecordsViewController.title = @"通话记录";
        imgIcon = [UIImage imageNamed:@"icon_record.png"];
        item = [[UITabBarItem alloc] initWithTitle:@"拨号" image:imgIcon tag:4];
        //        [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
        //           withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
        self.callRecordsViewController.tabBarItem = item;
        [item release];
        
        m_moreViewController = [[HCMoreViewController alloc] init];
        self.moreViewController.title = @"我";
        imgIcon = [UIImage imageNamed:@"nav_me.png"];
        item = [[UITabBarItem alloc] initWithTitle:@"我" image:imgIcon tag:5];
        //        [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
        //           withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
        self.moreViewController.tabBarItem = item;
        [item release];
        
        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.delegate = self;
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.callListViewController, self.CRMViewController, self.kpiViewController, self.callRecordsViewController, self.moreViewController, nil];
        self.tabBarController.customizableViewControllers = [NSArray arrayWithObjects:self.callListViewController, self.CRMViewController, self.kpiViewController, self.callRecordsViewController, self.moreViewController, nil];
        
        
        CGRect rect = self.tabBarController.view.frame;
        if (!IOS_VERSION_7_OR_ABOVE) {//Eonman 改1
            rect.origin.y -= 20;
        }
        self.tabBarController.view.frame = rect;
    }
    
//    [self presentViewController:self.tabBarController animated:YES completion:nil];
    [self.view addSubview:self.tabBarController.view];
}

////////////////////////////////////////////////////////////////////////////
#pragma mark 左边菜单
////////////////////////////////////////////////////////////////////////////
- (void)setMenuView:(UIView *)menuView
{
    if (self.menuView == menuView)
    {
        return;
    }
    
    [self.menuView removeFromSuperview];
    
    [menuView retain];
    [m_menuView release];
    m_menuView = menuView;
    
    if (self.menuView.superview == nil)
    {
        CGRect menuViewRect = self.menuView.frame;
        if (!IOS_VERSION_7_OR_ABOVE) {
            menuViewRect.origin.y = -20;
        }
        menuViewRect.origin.x = 0;
        self.menuView.frame = menuViewRect;
        self.menuView.clipsToBounds = YES;
    }
}

- (void)showOrCloseMenu
{
    if (self.isShowMenu)
    {
        [self closeMenu];
    }
    else
    {
        [self showMenu];
    }
}

- (void)showMenu
{
    if (self.menuView.subviews != nil)
    {
        [self.menuView removeFromSuperview];
        [self.view addSubview:self.menuView];
    }
    else
    {
        [self.view addSubview:self.menuView];
    }
    
    CGRect menuViewRect = self.menuView.frame;
    menuViewRect.origin.x = menuViewRect.size.width * -1;
    self.menuView.frame = menuViewRect;
    
    self.tabBarController.selectedViewController.view.clipsToBounds = NO;
    
    [UIView beginAnimations:nil context:nil];
    
    menuViewRect = self.menuView.frame;
    menuViewRect.origin.x = 0;
    self.menuView.frame = menuViewRect;
    
    CGRect rect = self.tabBarController.view.frame;
    rect.origin.x += self.menuView.frame.size.width;
    self.tabBarController.view.frame = rect;
    
    [UIView commitAnimations];
    
    m_isShowMenu = YES;
}

- (void)closeMenu
{
    [UIView beginAnimations:nil context:nil];
    
    CGRect menuViewRect = self.menuView.frame;
    menuViewRect.origin.x = menuViewRect.size.width * -1;
    self.menuView.frame = menuViewRect;
    
    CGRect rect = self.tabBarController.view.frame;
    rect.origin.x -= self.menuView.frame.size.width;
    self.tabBarController.view.frame = rect;
    
    [UIView commitAnimations];
    
    m_isShowMenu = NO;
}

@end
